Ext.define('Zm.view.x3d.Property',{
    alias:'widget.x3dproperty',
    extend:'Ext.tree.Panel',
    uses:[
        'Zm.i18n.Lang',
        'Ext.ux.TreePicker',
        'Ext.util.TaskManager',
        'Ext.grid.plugin.CellEditing'
    ],
    
    initComponent: function(){
        var me = this,
            cm = Zm.Common,
            lang = Zm.i18n.Lang,
            fn = Ext.bind(me.refdata,me);
        me.cellEditing = Ext.create('Ext.grid.plugin.CellEditing',{errorSummary:false});
        me.editors = new Ext.util.HashMap();
        me.taskRef = Ext.util.TaskManager.newTask({
            run:fn,
            interval:1000
        });
        Ext.apply(me,{
            multiSelect:true,
            plugins: [me.cellEditing],
            rootVisible:false,
            rowLines:true,
            resizable:true,
            columnLines:true,
            store:'DevAttr',
            columns:[
                {xtype:'treecolumn',text:lang.tAttrName,width:140,dataIndex:'attrname'},
                {text:lang.tAttrVal,dataIndex:'defval',flex:1,renderer:me.rendVal,getEditor:me.getCellEditor}
            ],
            listeners:{
                edit:me.onEdit
            }
        });
        me.addEvents('propertychange');
        me.callParent(arguments);
        
        me.taskRef.start();
    },
    getCellEditor:function(record){
        var me = this.up('x3dproperty'),
            vtid = record.get('vtid'),
            typedata = {vtid:vtid,editor:record.get('editor')},
            editor = null;
        editor = me.editors.get(vtid);
        if(editor === undefined){
            editor = Zm.Common.getEditor(typedata,true);
            me.editors.add(vtid,editor);
        }
        return editor;
    },
    beforeDestroy: function() {
        var me = this,editor;
        me.callParent();
        for(editor in me.editors){
            Ext.destroy(editor);
        }
    },
    setValue:function(devid){
        var me = this,
            proxy = me.store.getProxy();
        proxy.extraParams = {devid:devid};
        me.store.load({
            callback: me.onStoreLoad,
            scope:me
        });
    },
    onStoreLoad:function(){
        var me = this,
            rootNode = me.store.getRootNode(),
            node = rootNode.firstChild;
        if(node){
            me.up('#epanel').setTitle(node.raw.devname);
        }
    },
    rendVal:function(val,meta,record,rowIndex,colIndex,store,view){
        return record.get('valdesc');
    },
    onEdit:function(editor,e,eOpt){
        var me = this;
        me.fireEvent('propertychange',me,editor,e,eOpt);
    },
    getDataId:function(node){
        var me = this,s = '',i,childNode,sc;
        if(node.isExpanded()){
            for(i=0;i<node.childNodes.length;i++){
                childNode = node.childNodes[i];
                if(childNode.get('editor')=='data'){
                    if(s.length>0) s += ',';
                    s += '{"id":"' + childNode.internalId + '","val":"' + childNode.get('vval') + '"}';
                }
                sc = me.getDataId(childNode);                   
                if(sc.length>0) s += ',' + sc;
            }
        }
        return s;
    },
    refdata:function(){
        var me = this,ids;
        ids = me.getDataId(me.getRootNode());
        if(ids.length>0){
            Ext.Ajax.request({
                url: g_app + '/Data/getdata',
                params: {ids:ids},
                scope: me,
                success:function(response,options){ 
                     var obj = Ext.decode(response.responseText,true);
                     if(obj==null) return;
                     if(obj.success){
                        me.upDat(obj.data);
                     }
                },
                failure:Zm.Common.onAFail
             });                
        }
    },
    upDat:function(data){
        var me = this,i,d,node;
        for(i=0;i<data.length;i++){
            d = data[i];
            node = me.store.getNodeById(d.id);
            if(node){
                node.set('valdesc',d.val);
                node.commit();
            }
        }
    }
});